package com.example.demo.Util;

/**
 * 代码处理工具类
 * @author admit
 *
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class CodeUit {

	/**
	 * 获取当前时间 设置格式为 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public String getDatetimeNow() {

		Date currentTime = new Date();
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(currentTime);
		return time;

	}

	/**
	 * 截取字符串获取时间格式设置为 HH：mm格式
	 * 
	 * @param time
	 * @return
	 */
	public String getTime(Timestamp time) {
		String tmpTime = time.toString();
		String hhmm = tmpTime.substring(tmpTime.length() - 10,
				tmpTime.length() - 5);

		return hhmm;

	}

	/**
	 * 截取字符串获取时间格式设置为yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param time
	 * @return
	 */
	public String getTimeMdy(Timestamp time) {
		String tmpTime = time.toString();
		String hhmm = tmpTime.substring(0, tmpTime.length() - 2);
		return hhmm;

	}
	

	/**
	 * 获取当前时间格式为yyyyMMddHHmmssSSS 云之讯短信时间格式
	 * 
	 * @return
	 */
	public String getDatetimeNowMSg() {

		Date currentTime = new Date();
		String time = new SimpleDateFormat("yyyyMMddHHmmssSSS")
				.format(currentTime);
		return time;

	}

	/**
	 * MD5加密处理工具类
	 */

	public String getMd5(String plainText, int langs) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			if (langs == 32) {
				// 32位加密
				return (buf.toString()).toUpperCase();
			} else {
				// 16位的加密
				return buf.toString().substring(8, 24);
			}

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}

	}

	/*
	 * 返回长度为strLength的随机数，在前面补0
	 */
	public String getFixLenthString(int strLength) {

		Random rm = new Random();

		// 获得随机数
		double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

		// 将获得的获得随机数转化为字符串
		String fixLenthString = String.valueOf(pross);

		// 返回固定的长度的随机数
		return fixLenthString.substring(1, strLength + 1);
	}

	public String GetUserIpAddress(HttpServletRequest request,
                                   HttpServletResponse response) throws ServletException, IOException {

		String remoteAddr = request.getRemoteAddr();
		String forwarded = request.getHeader("X-Forwarded-For");
		String realIp = request.getHeader("X-Real-IP");

		String ip = null;
		if (realIp == null) {
			if (forwarded == null) {
				ip = remoteAddr;
			} else {
				ip = remoteAddr + "/" + forwarded.split(",")[0];
			}
		} else {
			if (realIp.equals(forwarded)) {
				ip = realIp;
			} else {
				if (forwarded != null) {
					forwarded = forwarded.split(",")[0];
				}
				ip = realIp + "/" + forwarded;
			}
		}
		return ip;
	}
}
